#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initial();

    timeOfFucked = 0;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initial()
{


    setWindowFlag(Qt::WindowCloseButtonHint);

    ui->label->setText(QString("<font size = '30'>谁动了我的代码？！</font>"));
    ui->label_2->setText(QString("<font size = '15' color = red>传入cpp/h文件</font>"));
}

void MainWindow::on_pushButton_clicked()
{
    fileName = QFileDialog::getOpenFileName(this, "select code file(s)", "...", "Code File(*.cpp;*.h)");
    if(fileName != ""){
        readAndReplace(fileName);

        timeOfFucked = 0;
    }
}

void MainWindow::readAndReplace(QString str)
{
    a = new AnalysingThread(str);

    a->start();
    a->wait();

    timeOfFucked = a->getNumOfFucked();

    if(timeOfFucked == 0)
        QMessageBox::information(this, "可喜可贺!","传入的代码没有被恶意替换", QMessageBox::Ok);
    else {
        QMessageBox::warning(this, "糟了", QString("你的代码有%1处受到恶意替换，已经将其改正后保存。请立刻干死上一个使用你的电脑的人=w=").arg(timeOfFucked), QMessageBox::Ok);
    }
    delete a;
}
